// pages/user/user/userInterest/userInterest.js
const app = getApp();
// 引入SDK核心类
var QQMapWX = require('../../../../utils/qqmap-wx-jssdk.min.js');
var key = app.globalData.qqmapsdk;
var qqmapsdk = new QQMapWX({
  key: key
});
import Api from '../../../../utils/http';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchFlag: true,
    follow: 1, //1关注状态,2取关
    dataList: [], //最近列表
    followidx: '',
    code: '', //最近接口判断码
    oftenCode: '', //常用接口判断码
  },
  // //关注
  // addfollow: function (e) {
  //   var that = this;
  //   console.log(that.data);
  //   that.getFollow(e);
  //   that.setData({
  //     follow:2
  //   })
  //   wx.showToast({
  //     title: '关注成功',
  //     icon: 'success',
  //     duration: 1000
  //   })
  // },
  //取消关注
  celfollow: function (e) {
    var that = this;
    console.log(that.data);
    that.getFollow(e);
    setTimeout(function(){
      that.getPoinIndexDetail();
    },1000)
    wx.showToast({
      title: '成功取消关注',
      icon: 'success',
      duration: 1000
    })
  },


  //投放点详情
  toPointDetail: function (e) {
    var that = this
    var {
      dataList
    } = that.data
    let index = e.currentTarget.dataset.index
    var lid = dataList[index].id
    var longitude = dataList[index].longitude
    var latitude = dataList[index].latitude
    wx.navigateTo({
      url: `/pages/user/putInPoint/pointDetail/pointDetail?lid=${lid}&longitude=${longitude}&latitude=${latitude}`,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.getCurrentLocation();
    
    // that.getMyLocation();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },
  //关注
  getFollow: function (e) {
    var that = this;
    let uid = wx.getStorageSync('userid')
    let followtype = e.currentTarget.dataset.type;
    console.log(e)
    var param = {
      uid: uid.id,
      lid: followtype,
      type: e.currentTarget.dataset.index,
    }
    console.log(param)
    Api.ajax("/app/Zarticle/follow", param).then(res => {
      console.log(res)
      if (res.code == 200) {
        
      }
    });
  },
  // 关注列表
  getPoinIndexDetail: function () {
    var that = this;
    let uid = wx.getStorageSync('userid');
    var param = {
      uid: uid.id,
      user_type: uid.user_type,
      longitude: that.data.lng,
      latitude: that.data.lat,
      pageNum: '1',
      pageSize: '9'
    }
    console.log(param)
    Api.ajax("/app/Zarticle/followList", param).then(res => {
      console.log(res, "list")
      if (res.code == 200) {
        this.setData({
          dataList: res.data,
          code: res.code
        });
      }else{
        this.setData({
          dataList: res.data,
          code: res.code
        });
      }
    })
  },

  // 获取当前定位
  getCurrentLocation: function () {
    var _this = this;
    qqmapsdk.reverseGeocoder({
      //位置坐标，默认获取当前位置，非必须参数
      //Object格式
      // location: {
      //   latitude: 39.984060,
      //   longitude: 116.307520
      // },
      //String格式
      // location: '39.984060,116.307520',

      //location: '', //获取表单传入的位置坐标,不填默认当前位置,示例为string格式
      // get_poi: 1, //是否返回周边POI列表：1.返回；0不返回(默认),非必须参数
      success: function (res) { //成功后的回调
        console.log(res);
        var result = res.result;
        wx.setStorageSync('latLng', result.location)
        _this.setData({
          lat: result.location.lat,
          lng: result.location.lng,
        })
        _this.getPoinIndexDetail();
      },
      fail: function (error) {
        console.error(error);
      },
      complete: function (res) {
        // console.log(res);
        wx.hideLoading();
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that=this;
    that.getPoinIndexDetail();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})